package com.googlecode.jsimplemarket.wicket.zzz;

import com.googlecode.jsimplemarket.model.Utilizator;
import com.googlecode.jsimplemarket.model.ZZZ;
import com.googlecode.jsimplemarket.wicket.SecuredWebSession;

import com.googlecode.jsimplemarket.wicket.zzz.ZZZListPage;
import com.googlecode.jsimplemarket.service.GenericService;

import com.googlecode.jsimplemarket.wicket.pages.DefaultEditPage;


import java.util.Date;
//import java.util.ArrayList;
//import java.util.List;
import java.math.BigDecimal;

import org.apache.wicket.extensions.markup.html.form.DateTextField;
import org.apache.wicket.extensions.yui.calendar.DatePicker;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;


import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;

import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 *
 * @author Ionel.David
 */


public class ZZZAddPage extends DefaultEditPage {
    @SpringBean(name = "genericService")
    private GenericService<ZZZ> zzzService;
    
    private ZZZ zzz;
    
    protected Utilizator getUtilizator() {
        return ((SecuredWebSession) getSession()).getUserDetail().getUtilizator();
    }

        
    public ZZZAddPage() {
    	
    	final WebMarkupContainer wmcFeedback = new WebMarkupContainer("wmcFeedback");
    	wmcFeedback.setOutputMarkupId(true);
    	wmcFeedback.setVisible(false);
    	
    	FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
    	wmcFeedback.add(feedbackPanel);
    	
    	add(wmcFeedback);
    	
        zzz = new ZZZ();
        zzz.setDataAdaugarii(new Date());
        
        Form<ZZZ> form = new Form<ZZZ>("form");
        form.setModel(new CompoundPropertyModel<ZZZ>(zzz));
    
        DateTextField dtfDataAdaugarii = new DateTextField("dataAdaugarii", "dd.MM.yyyy");
        dtfDataAdaugarii.add(new DatePicker());
        dtfDataAdaugarii.setRequired(true);
        form.add(dtfDataAdaugarii);
        
        TextField<BigDecimal> tfValoare = new TextField<BigDecimal>("valoareTotala");
        tfValoare.setRequired(true);
        form.add(tfValoare);
        
        TextArea<String> tfDenumire = new TextArea<String>("denumire");
        tfDenumire.setRequired(true);
        form.add(tfDenumire);
        
        SubmitLink salvare = new SubmitLink("submit") {

            @Override
            public void onSubmit() {
                if (zzz.getId() == null) {
                	zzz.setUtilizator(getUtilizator());
                    zzzService.insert(zzz);
                } else {
                	zzz.setUtilizator(getUtilizator());
                    zzzService.update(zzz);
                }
                setResponsePage(ZZZListPage.class);
            }
            
            @Override
            public void onError() {
            	
            	 //wmcFeedback.setVisible(true);
            	 info("Formularul a fost completat cu eroari!");
            }
        };
        form.add(salvare);
        
        form.add(new Link("renuntare") {

            @Override
            public void onClick() {
                setResponsePage(new ZZZListPage());
            }
            
        });

        add(form);
    }
}
